package com.fjh.shop.goods.service.controller;

import com.fjh.shop.common.Result;
import com.fjh.shop.goods.model.entity.GoodsImage;
import com.fjh.shop.goods.service.service.GoodsImageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  商品图片前端控制器
 * </p>
 *
 * @author wuyunbin
 * @since 2025-07-16
 */
@Tag(name = "商品图片管理", description = "商品图片相关接口")
@Slf4j
@RestController
@RequestMapping("/goodsImage")
public class GoodsImageController {

    @Resource
    private GoodsImageService goodsImageService;

    /**
     * 添加商品图片
     * @param goodsImage 商品图片信息
     * @return 添加结果
     */
    @Operation(summary = "添加商品图片", description = "添加新的商品图片信息")
    @ApiResponse(responseCode = "200", description = "添加成功")
    @PostMapping("")
    public Result<Object> add(@RequestBody GoodsImage goodsImage) {
        log.info("添加商品图片：{}", goodsImage);
        goodsImageService.save(goodsImage);
        return Result.success();
    }

    /**
     * 根据ID查询商品图片
     * @param id 商品图片ID
     * @return 商品图片信息
     */
    @Operation(summary = "根据ID查询商品图片", description = "根据商品图片ID查询详细信息")
    @ApiResponse(responseCode = "200", description = "查询成功")
    @GetMapping("/{id}")
    public Result<GoodsImage> findById(@Parameter(description = "商品图片ID") @PathVariable Long id) {
        log.info("根据ID查询商品图片：{}", id);
        GoodsImage goodsImage = goodsImageService.getById(id);
        return Result.success(goodsImage);
    }

    /**
     * 更新商品图片
     * @param goodsImage 商品图片信息
     * @return 更新结果
     */
    @Operation(summary = "更新商品图片", description = "更新已有的商品图片信息")
    @ApiResponse(responseCode = "200", description = "更新成功")
    @PutMapping("")
    public Result<Boolean> update(@RequestBody GoodsImage goodsImage) {
        log.info("更新商品图片：{}", goodsImage);
        boolean result = goodsImageService.updateById(goodsImage);
        return Result.success(result);
    }

    /**
     * 删除商品图片
     * @param id 商品图片ID
     * @return 删除结果
     */
    @Operation(summary = "删除商品图片", description = "根据ID删除商品图片")
    @ApiResponse(responseCode = "200", description = "删除成功")
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@Parameter(description = "商品图片ID") @PathVariable Long id) {
        log.info("删除商品图片：{}", id);
        boolean result = goodsImageService.removeById(id);
        return Result.success(result);
    }
}
